/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pesqBairro.java
 *
 * Created on 05/01/2010, 20:06:04
 */

package cparc;

import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrador
 */
public class pesqBairro extends javax.swing.JInternalFrame  {
    
   private static boolean prop = false;
    private static boolean rua = false;
    private static boolean anim = false;
    private static boolean ruabai = false;
    static cadastroProp frameProp;
    static cadastroRuas framePropRua;


    public void setProp(cadastroProp fProp){

       prop = true;
       frameProp = fProp;
       ruabai = false;
    }

     public void setPropRua(cadastroRuas fProp){

       ruabai = true;
       framePropRua = fProp;
       prop = false;
    }

    /** Creates new form pesqBairro */
    public pesqBairro() {
        initComponents();

        tfConsulta.setText("Digite o texto aqui...");
        tfConsulta.requestFocusInWindow();
        tfConsulta.selectAll();

        DefaultTableModel modelo = (DefaultTableModel) tbBai.getModel();
        Statement stm = null;
        String result = null;
        String consulta = "SELECT BCOD, BNOM FROM CPARC.CAD_BAI ORDER BY BCOD ASC";
        int i=0;

        try  {
        stm = (Statement) conexao.getConnection(true).createStatement();
        ResultSet rs = stm.executeQuery(consulta);

        while(rs.next()){
        modelo.addRow(new String[] {Integer.toString(rs.getInt("BCOD")), rs.getString("BNOM")});
        modelo.fireTableCellUpdated(i, 0);
        modelo.fireTableCellUpdated(i, 1);
        i++;
        }

        }
        catch(Exception e){
            String status = e.getMessage();
            System.out.println(status);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tfConsulta = new javax.swing.JTextField();
        btConsulta = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbBai = new javax.swing.JTable();

        setTitle("Pesquisa de Bairros");

        tfConsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfConsultaKeyPressed(evt);
            }
        });

        btConsulta.setText("Consultar");
        btConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConsultaActionPerformed(evt);
            }
        });

        tbBai.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Bairro"
            }
        ));
        tbBai.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbBaiKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbBai);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(tfConsulta, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btConsulta))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btConsulta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConsultaActionPerformed
        
        System.out.println("Botao");
        DefaultTableModel modelo = (DefaultTableModel) tbBai.getModel();
        modelo.setRowCount(0);
        Statement stm = null;
        String result = null;
        String consulta = "SELECT BCOD, BNOM FROM CPARC.CAD_BAI WHERE BNOM LIKE '%" + tfConsulta.getText() + "%' ORDER BY BNOM ASC";
        int i=0;
        System.out.println(consulta);
        try  {
        stm = (Statement) conexao.getConnection(true).createStatement();
        ResultSet rs = stm.executeQuery(consulta);

        while(rs.next()){
        modelo.addRow(new String[] {Integer.toString(rs.getInt("BCOD")), rs.getString("BNOM")});
        modelo.fireTableCellUpdated(i, 0);
        modelo.fireTableCellUpdated(i, 1);
        i++;
        }

        }
        catch(Exception e){
            e.printStackTrace();
        }

        tbBai.requestFocusInWindow();
    }//GEN-LAST:event_btConsultaActionPerformed

    private void tfConsultaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfConsultaKeyPressed

        if(evt.getKeyCode() == 10){

            btConsulta.doClick();
        }

        else if(evt.getKeyCode() == 27){


            if(prop)
            {
                this.dispose();
                frameProp.requestFocusInWindow();
                frameProp.escPesqBairro();
            }
            if(ruabai)
            {
                this.dispose();
                framePropRua.requestFocusInWindow();
                framePropRua.escPesqBairro();
            }
        }
    }//GEN-LAST:event_tfConsultaKeyPressed

    private void tbBaiKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbBaiKeyPressed

        if(evt.getKeyCode() == 10 && prop == true ){

            this.dispose();
            frameProp.requestFocusInWindow();
            frameProp.bairro((String) tbBai.getValueAt(tbBai.getSelectedRow(), 0), (String) tbBai.getValueAt(tbBai.getSelectedRow(), 1));
            prop = false;

        }else if(evt.getKeyCode() == 10 && ruabai == true ){

            this.dispose();
            framePropRua.requestFocusInWindow();
            framePropRua.bairro((String) tbBai.getValueAt(tbBai.getSelectedRow(), 0), (String) tbBai.getValueAt(tbBai.getSelectedRow(), 1));
            ruabai = false;
        }
    }//GEN-LAST:event_tbBaiKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JButton btConsulta;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTable tbBai;
    public javax.swing.JTextField tfConsulta;
    // End of variables declaration//GEN-END:variables

}
